<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform @switchChange="switchChange" ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		errorModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		changeConfigAttr,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';

	export default {
		data() {
			return buildFormData({
				customerCacheKey:'rps-xz-customer-customer-cache',
				customerPartitionCacheKey:'rps-xz-customer-customerPartition-cache',
				addressCacheKey:'rps-xz-customer-address-cache',
				countryCacheKey:'rps-xz-customer-country-cache',
				industryCacheKey:'rps-xz-customer-industry-cache',
				customerTypeCacheKey:'rps-xz-customer-customerType-cache',
				saleProductCategoryCacheKey:'rps-xz-customer-saleProductCategory-cache',
				shippingMethodCacheKey:'rps-xz-customer-shippingMethod-cache',
				freightCacheKey:'rps-xz-customer-freight-cache',
				settlementMethodCacheKey:'rps-xz-customer-settlementMethod-cache',
				paymentTermCacheKey:'rps-xz-customer-paymentTerm-cache',
				currencyTypeCacheKey:'rps-xz-customer-currencyTypeId-cache',
				customerLevelCacheKey:'rps-xz-customer-customerLevel-cache',
				creditLevelCacheKey:'rps-xz-customer-creditLevel-cache',
				customerInstCacheKey:'rps-xz-customer-inst-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		methods: {
			switchChange(e,name){
				let _that = this
				if(name == 'domesticCustomer'){
					if(_that.$refs.autoform.formdata.abroadCustomer){
						_that.$refs.autoform.formdata.abroadCustomer = !_that.$refs.autoform.formdata.abroadCustomer
					}
				}
				if(name == 'abroadCustomer'){
					if(_that.$refs.autoform.formdata.domesticCustomer){
						_that.$refs.autoform.formdata.domesticCustomer = !_that.$refs.autoform.formdata.domesticCustomer
					}
				}
			},
			getMaxItemCode(id,code){
				let _that = this
				_that.om.request(
					"customer/getMaxItemCode",
					{
						customerClassId: id,
						customerClassCode: code
					}
				).then(res=>{
					_that.$refs.autoform.formdata.customerCode = res.value.customerCode
					_that.$refs.autoform.formdata.customerCodeNum = res.value.customerCodeNum
				})
			},
			initLoad(options) {
				let _that = this;
				var fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'customer/getCustomer',
						param:{
							code:'38985'
						}
					},
					{
						url:'customer/getCustomer',
						param:{
							code:'38987'
						}
					},
					{
						url:'customer/getCustomer',
						param:{
							code:'38988'
						}
					},
					{
						url:'dictZero/findYesNoList',
					},
					{
						url:'dictSource/loadCombo',
						param:{
							sourceId:'283',
							categoryId:'6431'
						}
					}
					
				]
				if(fileId){
					ajaxArr.push({
						url:'customer/getValue',
						param:{
							fileId:fileId
						}
					})
					
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posName"),mainData)
					changeEditor("productInfo","customerClassId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify(),
							url:'customer/getCustomerClass',
							initload:true,
							namefield:'typeName',
							extrafield:'categoryCode',
							notefield:'parentId',
							cachename:_that.customerCacheKey
						}
					},null,mainData)
					changeEditor("productInfo","productPositionId","data",configSelectData(res[1],"id","dicname"),mainData)
					changeEditor("productInfo","storeSalesModeId","data",configSelectData(res[2],"id","dicname"),mainData)
					changeEditor("productInfo","storeTypeId","data",configSelectData(res[3],"id","dicname"),mainData)
					
					changeEditor("title1","limitPinControlId","data",configSelectData(res[4],"value","name"),mainData)
					changeEditor("title1","limitCreditControlId","data",configSelectData(res[4],"value","name"),mainData)
					changeEditor("title1","termCreditControlId","data",configSelectData(res[4],"value","name"),mainData)
					
					changeEditor("productInfo","isPotentialCustomerId","data",configSelectData(res[5],"id","text"),mainData)
					changeEditor("productInfo","sharingInstId","data",configSelectData(res[5],"id","text"),mainData)
					
					
					changeEditor("productInfo","registeredCapital",'suffixCol',2,mainData)
					changeEditor("productInfo","customerPartitionId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify(),
							url:'customer/getCustomerPartition',
							namefield:'typeName',
							extrafield:'code',
							notefield:'parentName',
							initload:true,
							searchfield:'q',
							cachename:_that.customerPartitionCacheKey,
						}
					},null,mainData)
					changeEditor("productInfo","countryId",{
						link:'../../public/pageSearch/pageSearch',
						"linkParam":{
							other:JSON.stringify(),
							url:'regions/getCountryList',
							namefield:'dicname',
							initload:true,
							cachename:_that.countryCacheKey,
						}
					},null,mainData)
					
					changeEditor('productInfo', 'locationId','type','areabox',mainData)
					changeEditor('productInfo', 'locationId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
								classifyCode: '37056'
							}),
							searchfield: 'q',
							namefield: urlUtil.addressUrl().namefield,
							url: urlUtil.addressUrl().url,
							fullName:true,
							cachename: _that.addressCacheKey,
							placeholder: '请输入地址'
						}
					},null,mainData)
					changeEditor('productInfo', 'industryId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
								classifyCode: '37056'
							}),
							searchfield: 'q',
							namefield: 'typeName',
							url: 'customer/getIndustryClassification',
							cachename: _that.industryCacheKey,
							initload:true,
						}
					},null,mainData) 
					changeEditor('productInfo','customerLevelId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							url:'customer/getCustomerClassification',
							cachename: _that.customerLevelCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('productInfo','customerTypeId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							url:'customer/getCustomerForm',
							cachename: _that.customerTypeCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('productInfo','saleProductCategoryIdStr', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							url:'customer/getSaleProductCategory',
							cachename: _that.saleProductCategoryCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('title1','shippingMethodId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							notefield:'typeCode',
							extrafield:'transceiverFlagName',
							url:'customer/getShippingMethod',
							cachename: _that.shippingMethodCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('title1','freightId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							notefield:'typeCode',
							extrafield:'transceiverFlagName',
							url:'customer/getFreight',
							cachename: _that.freightCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('title1','settlementMethodId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							notefield:'typeCode',
							extrafield:'transceiverFlagName',
							url:'customer/getSettlementMethod',
							cachename: _that.settlementMethodCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('title1','paymentTermId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'typeName',
							notefield:'typeCode',
							extrafield:'transceiverFlagName',
							url:'customer/getPaymentTerm',
							cachename: _that.paymentTermCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('title1','currencyTypeId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
							}),
							searchfield: 'q',
							namefield: 'currencyName',
							notefield:'currencyCode',
							extrafield:'currencyCoding',
							url:'currencyDict/getCurrency',
							cachename: _that.currencyTypeCacheKey,
							initload:true,
						}
					},null,mainData)
					changeEditor('title1','creditLevelId', {
						link: '../../public/pageSearch/pageSearch',
						"linkParam":{
							other: JSON.stringify({
								code:'39170'
							}),
							searchfield: 'q',
							namefield: 'dicname',
							url:'customer/getCustomer',
							cachename: _that.creditLevelCacheKey,
							initload:true,
						}
					},null,mainData)
					
					changeEditor('productInfo','customerInstId', {
						link: '../../public/search/search',
						"linkParam":{
							other: JSON.stringify({
							}),
							namefield: 'text',
							url:'tinstitution/getComboTree',
							cachename: _that.customerInstCacheKey,
							initload:true,
						}
					},null,mainData)
					if (_that.subConfigData["title6"]) {
						let configData = _that.subConfigData["title6"]
						let attr = {}  
						if (hasEditor('title6', 'customerOwnerId', configData)) {
							let title = getEditorTitle('title6', 'customerOwnerId', configData)
							attr.titleTemplate = title+':<%= customerOwner %>'
						}
						changeEditor('title6', 'ownerStr', attr, mainData);
					}
					if (_that.subConfigData["title3"]) {
						let configData = _that.subConfigData["title3"]
						let attr = {}  
						if (hasEditor('title3', 'issuingOfficeCode', configData)) {
							let title = getEditorTitle('title3', 'issuingOfficeCode', configData)
							attr.titleTemplate = title+':<%= issuingOfficeCode %>'
						}
						changeEditor('title3', 'invoicStr', attr, mainData);
					}
					if (_that.subConfigData["title4"]) {
						let configData = _that.subConfigData["title4"]
						let attr = {}  
						if (hasEditor('title4', 'bankId', configData)) {
							let title = getEditorTitle('title4', 'bankId', configData)
							attr.titleTemplate = title+':<%= bank %>'
						}
						changeEditor('title4', 'openStr', attr, mainData);
					}
					if (_that.subConfigData["title5"]) {
						let configData = _that.subConfigData["title5"]
						let attr = {}  
						if (hasEditor('title5', 'fileTypeId', configData)) {
							let title = getEditorTitle('title5', 'fileTypeId', configData)
							attr.titleTemplate = title+':<%= fileTypeName %>'
						}
						changeEditor('title5', 'uploadStr', attr, mainData);
					}
					if (_that.subConfigData["title2"]) {
						let configData = _that.subConfigData["title2"]
						let attr = {}  
						if (hasEditor('title2', 'userName', configData)) {
							let title = getEditorTitle('title2', 'userName', configData)
							attr.titleTemplate = title+':<%= userName %>'
						}
						changeEditor('title2', 'dataStr', attr, mainData);
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id",'customerCodeNum')
						for(let index in res[6].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[6].value[index])
								_that.$set(_that.formdata,index,res[6].value[index])
							}
						}
						if(_that.subEditorObj["title5"]){
							let dataList = _that._.cloneDeep(res[6].value.customerUploadList)
							_that.subEditorObj["title5"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "uploadStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "uploadStr", JSON.stringify(dataList))
						}
						if(_that.subEditorObj["title2"]){
							let dataList = _that._.cloneDeep(res[6].value.customerDataResponseList)
							_that.subEditorObj["title2"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "dataStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "dataStr", JSON.stringify(dataList))
						}
						if(_that.subEditorObj["title3"]){
							let dataList = _that._.cloneDeep(res[6].value.customerInvoicList)
							_that.subEditorObj["title3"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "invoicStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "invoicStr", JSON.stringify(dataList))
						}
						if(_that.subEditorObj["title4"]){
							let dataList = _that._.cloneDeep(res[6].value.customerOpenList)
							_that.subEditorObj["title4"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "openStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "openStr", JSON.stringify(dataList))
						}
						if(_that.subEditorObj["title6"]){
							let dataList = _that._.cloneDeep(res[6].value.customerOwnerResponseList)
							_that.subEditorObj["title6"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "ownerStr", JSON.stringify(dataList))
							_that.$set(_that.formdata, "ownerStr", JSON.stringify(dataList))
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("productInfo","isPotentialCustomerId")
						_that.$refs.autoform.initSelectIndex("productInfo","sharingInstId")
					})
				}, options)
			},
			formSubmit() {
				let _that = this
				if(!_that.edit){
					_that.om.formSave("customer/doAddLxm",_that.formdata,_that)
				}else{
					_that.om.formSave("customer/doUpdateLxm",_that.formdata,_that)
				}
			}
		},
		onUnload(){
		//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
		//#endif
		},
		onLoad(options) {
			this.initValue.userinfoId = this.userId
			this.initLoad(options)
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.customerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerClassId = d.id
							formdata.customerClass = d.typeCodeName
							_that.getMaxItemCode(d.id,d.code)
							uni.removeStorage({
								key:_that.customerCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.customerPartitionCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.customerPartitionId = d.id
							formdata.customerPartition = d.typeCodeName
							uni.removeStorage({
								key:_that.customerPartitionCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key: _that.customerInstCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.customerInstId = d.id
							formdata.customerInstName = d.text
							uni.removeStorage({
								key: _that.customerInstCacheKey
							});
						}
					}
				}); 
				uni.getStorage({
					key: _that.addressCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							_that.$refs.autoform.$refs.areapop.close()
							uni.removeStorage({
								key: _that.addressCacheKey
							});
						}
					}
				}); 
				uni.getStorage({
					key: _that.countryCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.countryId = d.id
							formdata.country = d.dicname
							uni.removeStorage({
								key: _that.countryCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.industryCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.industryId = d.id
							formdata.industry = d.typeCodeName
							uni.removeStorage({
								key: _that.industryCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.customerTypeCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.customerTypeId = d.id
							formdata.customerType = d.typeCodeName
							uni.removeStorage({
								key: _that.customerTypeCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.saleProductCategoryCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.saleProductCategoryIdStr = d.id
							formdata.saleProductCategory = d.typeCodeName
							uni.removeStorage({
								key: _that.saleProductCategoryCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key: _that.shippingMethodCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.shippingMethodId = d.id
							formdata.shippingMethodName = d.typeCodeName
							uni.removeStorage({
								key: _that.shippingMethodCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key:_that.freightCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.freightId = d.id
							formdata.freight = d.typeCodeName
							uni.removeStorage({
								key: _that.freightCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.settlementMethodCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.settlementMethodId = d.id
							formdata.settlementMethod = d.typeCodeName
							uni.removeStorage({
								key: _that.settlementMethodCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.paymentTermCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.paymentTermId = d.id
							formdata.paymentTerm = d.typeCodeName
							uni.removeStorage({
								key: _that.paymentTermCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.currencyTypeCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.currencyTypeId = d.id
							formdata.currencyType = d.currencyName
							uni.removeStorage({
								key: _that.currencyTypeCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.customerLevelCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.customerLevelId = d.id
							formdata.customerLevel = d.typeCodeName
							uni.removeStorage({
								key: _that.customerLevelCacheKey
							});
						}
					}
				})
				uni.getStorage({
					key:_that.creditLevelCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							var formdata = _that.$refs.autoform.formdata;
							formdata.creditLevelId = d.id
							formdata.creditLevel = d.dicname
							uni.removeStorage({
								key: _that.creditLevelCacheKey
							});
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
       